import {
	mapGetters
} from 'vuex'
import Lockr from 'lockr'

// table组件
// import CRMLawcaseListHead from '@/components/table/CRMLawcaseListHead'
import CRMTableHead from '@/components/table/CRMLawcaseTableHead'
import CRMListHead from '@/components/table/new/CRMListHead'
import CRMOptionButton from '@/components/table/new/CRMOptionButton'
import CRMNavCell from '@/components/table/new/CRMNavCell'

// API
import {
	filedGetField
} from '@/api/setting/setting'
import {
	crmLawcaseIndex,
	crmLawcaseXingshi,
	crmLawcaseZixun,
	crmLawcaseFuwu,
	crmLawcaseLogIndex,
	crmLawcaseCourtIndex,
	crmLawcaseJudgmentIndex,
	crmLawcasePreservationIndex,
	crmLawcaseComplainIndex,
	crmLawcaseFocus
} from '@/api/lawcase/lawcase'

import moment from 'moment'
import {
	getDateFromTimestamp,
	moneyFormat
} from '@/utils'

export default {
	components: {
		CRMListHead,
		CRMTableHead,
		CRMOptionButton,
		CRMNavCell
	},
	computed: {
		...mapGetters(['allAuth'])
	},
	mounted() {
		var self = this
		/** 控制table的高度 */
		window.onresize = function() {
			var offsetHei = document.documentElement.clientHeight
			var removeHeight = self.filterArr.length > 0 ? 310 : 240
			self.tableHeight = offsetHei - removeHeight
		}
		this.dataText = "";
		this.getFieldList()
	},
	data() {
		return {
			// 加载动画
			loading: false,
			// 表的高度
			tableHeight: document.documentElement.clientHeight - 240,
			// 搜索内容
			search: '',
			// 场景筛选ID
			scene_id: '',
			// 场景名字
			status: '',
			// 高级筛选
			filterObj: {},
			// 筛选确定数据
			filterArr: [],
			// 表格数据
			list: [],
			// 字段数据
			fieldList: [],
			// 格式化规则
			formatterRules: {},
			// 页码
			currentPage: 1,
			pageSize: Lockr.get('crmPageSizes') || 15,
			pageSizes: [15, 30, 60, 100],
			total: 0,
			money: 0,
			sum_money: 0,
			// 排序
			sortData: {},
			// 表格为空的时候显示的文本
			dataText: '',
			/** 控制详情展示 */
			rowID: '', // 行信息
			rowType: '', // 详情类型
			showDview: false,
			tabsName: ''
		}
	},
	methods: {
		/** 获取列表数据 */
		getList() {
			this.loading = true
			var crmIndexRequest = this.getIndexRequest()
			var params = {
				page: this.currentPage,
				limit: this.pageSize,
				search_v: this.search
			}
			if (this.scene_id) {
				params.scene_id = this.scene_id
			}

			if (this.status) {
				params.status = this.status
			}

			if (this.sortData.order) {
				params.order_field = this.sortData.prop
				params.order_type = this.sortData.order == 'ascending' ? 'asc' : 'desc'
			}
			if (this.filterArr.length > 0) {
				params['heightSearch'] = this.filterArr
			}

			crmIndexRequest(params).then(res => {
				this.list = res.data.list.map(element => {
					element.show = false
					return element
				})
				this.dataText = "暂无数据"
				this.total = res.data.count
				this.money = res.data.money ? res.data.money : 0
				this.sum_money = res.data.sum_money ? res.data.sum_money : 0
				this.loading = false
			}).catch(() => {
				this.loading = false
			})
		},

		/** 勾选操作 */
		handleHandle(data) {
			if (data.type === 'get' ||
				data.type === 'transfer' ||
				data.type === 'delete' ||
				data.type === 'put_seas' ||
				data.type === 'lizhi') {
				this.showDview = false
			} else if (data.type === 'z_alloc') {
				return this.fenpei(data)
			}

			if (data.type !== 'edit') {
				this.getList()
			}
		},

		/** 搜索操作 */
		crmSearch(value) {
			this.search = value
			this.currentPage = 1
			if (this.fieldList.length) {
				this.getList()
			}
		},

		/** 页面头部操作 */
		listHeadHandle(data, type) {
			if (type == "status") {
				this.status = data
			} else {
				this.scene_id = data
			}

			this.getList()
		},

		/** 占位 */
		exportData(params) {
			// console.log(params);
			this.$refs.search.handleTypeDrop('out', params)
		},

		/** 筛选操作 */
		handleFilter(data) {
			// console.log(data);
			this.filterArr = data
			var offsetHei = document.documentElement.clientHeight
			var removeHeight = this.filterArr.length > 0 ? 310 : 240
			this.tableHeight = offsetHei - removeHeight
			this.currentPage = 1
			this.search = ''
			this.getList()
		},

		/** 字段排序*/
		sortChange(column, prop, order) {
			this.sortData = column
			this.getList()
		},

		/** 当选择项发生变化时会触发该事件*/
		handleSelectionChange(val) {
			this.selectionList = val // 勾选的行
			this.$refs.crmTableHead.headSelectionChange(val)
		},

		/**  更改每页展示数量*/
		handleSizeChange(val) {
			Lockr.set('crmPageSizes', val)
			this.pageSize = val
			this.getList()
		},

		/**  更改当前页数*/
		handleCurrentChange(val) {
			this.currentPage = val
			this.getList()
		},

		/** 获取字段 */
		getFieldList() {
			if (this.fieldList.length == 0) {
				var params = {
					types: 'crm_' + this.crmType,
					module: 'crm',
					action: 'index'
				}

				if (this.actiontype) {
					params.actiontype = this.actiontype
				}
				params.controller = this.crmType

				filedGetField(params)
					.then(res => {
						for (let index = 0; index < res.data.length; index++) {
							const element = res.data[index]
							/** 获取需要格式化的字段 和格式化的规则 */
							if (element.form_type === 'date') {
								this.formatterRules[element.field] = {
									formatter: function fieldFormatter(time) {
										if (time == '0000-00-00') {
											time = ''
										}
										if (time) {
											return moment(getDateFromTimestamp(time)).format(
												'YYYY-MM-DD'
											)
										}


									}
								}
							} else if (element.form_type === 'datetime') {
								this.formatterRules[element.field] = {
									formatter: function fieldFormatter(time) {
										if (time == 0 || !time) {
											return ''
										}
										return moment(getDateFromTimestamp(time)).format(
											'YYYY-MM-DD HH:mm:ss'
										)
									}
								}
							} else if (element.form_type === 'user' || element.form_type === 'user-multiple') {
								this.formatterRules[element.field] = {
									type: 'crm',
									formatter: function fieldFormatter(info) {
										if (info) {
											var content = ''
											for (let index = 0; index < info.length; index++) {
												const element = info[index]
												content = content + element.username + (index === (info
													.length - 1) ? '' : ',')
											}
											return content
										}
										return ''
									}
								}
							} else if (element.form_type === 'floatnumber') {
								this.formatterRules[element.field] = {
									type: 'floatnumber',
									formatter: function fieldFormatter(info) {
										return moneyFormat(info)
									}
								}
							} else if (element.form_type === 'select-multiple') {
								this.formatterRules[element.field] = {
									type: 'array',
									formatter: function fieldFormatter(info) {
										return info
									}
								}
							}

							var width = 0
							if (!element.width) {
								if (element.form_type == 'datetime') {
									width = 150
								} else if (element.form_type == 'textarea') {
									width = 350
								} else {
									width = 105
								}
							} else {
								width = element.width
							}

							this.fieldList.push({
								prop: element.field,
								label: element.name,
								width: width
							})
						}
						// 获取好字段开始请求数据
						this.getList()
					})
					.catch(() => {
						this.loading = false
					})
			} else {
				// 获取好字段开始请求数据
				this.getList()
			}
		},

		/** 格式化字段 */
		fieldFormatter(row, column) {
			// 如果需要格式化
			var aRules = this.formatterRules[column.property]
			if (aRules) {
				if (aRules.type === 'crm') {
					if (column.property) {
						return aRules.formatter(row[column.property + '_info']) || '--'
					} else {
						return ''
					}
				} else {
					return aRules.formatter(row[column.property]) || '--'
				}
			}
			return row[column.property] || '--'
		},

		/** 获取列表请求 */
		getIndexRequest() {
			switch (this.crmType) {
				case 'lawcase':
					if (this.actiontype == 7) {
						return crmLawcaseIndex
					} else if (this.actiontype == 8) {
						return crmLawcaseLogIndex
					} else if (this.actiontype == 9) {
						return crmLawcaseCourtIndex
					} else if (this.actiontype == 10) {
						return crmLawcaseJudgmentIndex
					} else if (this.actiontype == 11) {
						return crmLawcasePreservationIndex
					} else if (this.actiontype == 12) {
						return crmLawcaseComplainIndex
					}

					break;

				default:
					break;
			}

		},

		/** 列表操作 */
		handleRowClick(row, column, event) {
			if (column.type === 'selection') {
				return // 多选布局不能点击
			}
			let arr = [7]
			if (column.property === 'name') {
				if (arr.includes(this.actiontype)) {
					window.open('/#/lawcase/read/' + row.id)
					// this.$router.push({
					// 	path: "/lawcase/read/" + row.id
					// })
				} else {
					this.$router.push({
						path: "/lawcase/read/" + row.lawcase_id
					})
				}
			} else if (column.property === 'customer') {
				this.$router.push({
					path: "/customer/read/" + row.customer_id
				})
			}
		},

		searchHandle(item) {
			// console.log(item);
			this.filterArr = [{
				"search_v": item.value,
				"search_f": item.field,
				"search_t": item.form_type
			}]
			this.getList()
		},

		heightSearchHandle(item) {
			let arr = []
			let list = item.map((item) => {
				if (item.value != undefined) {
					arr.push({
						"search_v": item.value,
						"search_f": item.field,
						"search_t": item.form_type
					})
				}
			})
			this.filterArr = arr
			this.getList()
		},


		optionHandle() {},

		/** 关注/取消关注 */
		focus(item) {
			this.$confirm('确认进行此操作?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				})
				.then(() => {
					crmLawcaseFocus({
							is_focus: item.is_focus,
							id: [item.lawcase_id]
						})
						.then(res => {
							this.$message({
								type: 'success',
								message: res.data
							})
							this.getList()
						})
						.catch(() => {})
				})
				.catch(() => {
					this.$message({
						type: 'info',
						message: '已取消操作'
					})
				})
		}

	}
}